package com.example.demo.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.BackResponseEntity;
import com.example.demo.entity.BooksInformation;
import com.example.demo.entity.requestJson.DeleteReq;
import com.example.demo.entity.responseJson.HomepageAllBInfoRes;
import com.example.demo.enums.ErrCodeEnum;
import com.example.demo.mapper.BooksInformationMapper;
import com.example.demo.service.AuthorCreationService;
import com.example.demo.service.BookInformationShowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

/**
 * @author : 洪俊新
 * @date : 2021/3/9 11:52
 * @description:
 */
@Service
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class BookInformationShowServiceImpl implements BookInformationShowService {

    @Autowired
    private BooksInformationMapper booksInformationMapper;
    @Autowired
    private AuthorCreationService informationService;

    /**
     * 书籍详情页
     * */
    @Override
    public BackResponseEntity<HomepageAllBInfoRes> queryBookAllInfo(DeleteReq deleteReq){

        BooksInformation booksInformation = new BooksInformation();
        booksInformation.setBooksId(deleteReq.getId());
        LambdaQueryWrapper<BooksInformation> queryWrapper = new QueryWrapper<BooksInformation>().lambda();
        queryWrapper.select(BooksInformation::getBooksId,BooksInformation::getBookInfo,BooksInformation::getCommentCount,BooksInformation::getFavoritesCount)
                .eq(!ObjectUtils.isEmpty(deleteReq.getId()),BooksInformation::getBooksId,deleteReq.getId());

        BooksInformation list = booksInformationMapper.selectOne(queryWrapper);
        HomepageAllBInfoRes homepageAllBInfoRes = new HomepageAllBInfoRes();
        homepageAllBInfoRes.setBooksId(list.getBooksId());
        homepageAllBInfoRes.setBookInfo(list.getBookInfo());
        homepageAllBInfoRes.setCommentCount(list.getCommentCount());
        homepageAllBInfoRes.setFavoritesCount(list.getFavoritesCount());

        return BackResponseEntity.success(ErrCodeEnum.unknown_0,homepageAllBInfoRes,"");

    }
}
